using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Framework.Manager;
using Framework.Configuration;
using System.IO;
using Framework.Util;

namespace FrameworkEditor.UITools.UICommonSync
{
    public class UICommonSync : EditorWindow
    {
        private static string mCommonPath = "Game/GameResources/UIs/Common";
        private static List<string> mCommonFileList = new List<string>();
        private static List<string> mCommonFileNameList = new List<string>();

        private static string mTargetPath = "Game/GameResources/UIs";
        private static List<string> mTargetDirectoryList = new List<string>();

        [MenuItem("FrameworkEditor/UICommonSync")]
        public static void UICommonSyncFunc()
        {
            mCommonFileList = GetCommonFileList();
            mCommonFileNameList = GetCommonFileNameList();
            mTargetDirectoryList = GetTargetFileList();
            CommonSync();

            AssetDatabase.Refresh();
        }

        private static List<string> GetCommonFileList()
        {
            List<string> list = new List<string>();

            string commonPath = UtilCollection.PathUtil.LinkPath(Application.dataPath, mCommonPath);
            string[] files = Directory.GetFiles(commonPath);
            foreach (string file in files)
            {
                string filePath = file.Replace("\\", "/");
                list.Add(filePath);
            }

            return list;
        }

        private static List<string> GetCommonFileNameList()
        {
            List<string> list = new List<string>();

            foreach (string file in mCommonFileList)
            {
                string fileName = Path.GetFileName(file);
                list.Add(fileName);
            }

            return list;
        }

        private static List<string> GetTargetFileList()
        {
            List<string> list = new List<string>();

            string commonPath = UtilCollection.PathUtil.LinkPath(Application.dataPath, mCommonPath);
            string targetPath = UtilCollection.PathUtil.LinkPath(Application.dataPath, mTargetPath);
            string[] directories = Directory.GetDirectories(targetPath);
            foreach (string directory in directories)
            {
                string directoryPath = directory.Replace("\\", "/");
                if (directoryPath != commonPath)
                {
                    string childTargetPath = UtilCollection.PathUtil.LinkPath(directoryPath, "Textures");
                    string[] childDirectories = Directory.GetDirectories(childTargetPath);
                    foreach (string childDirectory in childDirectories)
                    {
                        string childDirectoryPath = childDirectory.Replace("\\", "/");
                        list.Add(childDirectoryPath);
                    }
                }
            }

            return list;
        }

        private static void CommonSync()
        {
            foreach (string commonFile in mCommonFileList)
            {
                string fileName = Path.GetFileName(commonFile);
                Debug.Log(commonFile);

                foreach (string targetDirectory in mTargetDirectoryList)
                {
                    string targetFilePath = UtilCollection.PathUtil.LinkPath(targetDirectory, fileName);
                    if (File.Exists(targetFilePath))
                    {
                        Debug.LogError(targetFilePath);
                        File.Copy(commonFile, targetFilePath, true);
                    }
                }
            }
        }
    }
}